﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// qt headers
#include <QVariant>
#include <QList>

// custom headers
#include "CustomTreeItem.h"


#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 
namespace gmepgcs
{
    CustomTreeItem::CustomTreeItem(const QList<QVariant>& data, CustomTreeItem * parentItem)
    {
        m_itemData = data;
        m_parentItem = parentItem;
    }

    CustomTreeItem::~CustomTreeItem()
    {
        qDeleteAll(m_childItems);
    }

    void CustomTreeItem::AppendChild(CustomTreeItem * child)
    {
        m_childItems.append(child);
    }

    CustomTreeItem * CustomTreeItem::ChildItem(int row) const
    {
        // QList will provide sensible default value if the row number is out of range.
        return m_childItems.value(row); 
    }

    int CustomTreeItem::ChildCount() const
    {
        return m_childItems.count();
    }

    CustomTreeItem * CustomTreeItem::ParentItem() const
    {
        return m_parentItem;
    }

    bool CustomTreeItem::RemoveChildren(int position, int count)
    {
        if (position < 0 || position + count > m_childItems.size())
        {
            return false;
        }

        for (int row = 0; row < count; ++row)
        {
            delete m_childItems.takeAt(position);
        }

        return true;
    }

    int CustomTreeItem::Row() const
    {
        if (m_parentItem)
        {
            return m_parentItem->m_childItems.indexOf(const_cast<CustomTreeItem*>(this));
        }

        return 0;
    }

    int CustomTreeItem::ColumnCount() const
    {
        return m_itemData.count();
    }

    QVariant CustomTreeItem::Data(int column) const
    {
        // QList will provide sensiable default value if the column number is out of range.
        return m_itemData.value(column); 
    }

    QString CustomTreeItem::ToString()
    {
        return Data(0).toString();
    }

    QString CustomTreeItem::ToFullString(const std::wstring & separator, const bool &includeRoot)
    {
        auto parentItem = ParentItem();
        auto grandParentItem = parentItem == nullptr ? nullptr : parentItem->ParentItem();

        if (includeRoot)
        {
            if (!parentItem)
            {
                return ToString();
            }
            else
            {
                return QString("%1%2%3")
                    .arg(parentItem->ToFullString(separator, includeRoot))
                    .arg(QString::fromStdWString(separator))
                    .arg(ToString());
            }
        }
        else
        {
            if (!parentItem)
            {
                return QString();
            }

            if (!grandParentItem)
            {
                return ToString();
            }

            return QString("%1%2%3")
                .arg(parentItem->ToFullString(separator, includeRoot))
                .arg(QString::fromStdWString(separator))
                .arg(ToString());
        }
    }

}